<?php
namespace app\klingai\controller;
use think\Db;
use app\model\KlingaiRecord as RecordModel;

use Firebase\JWT\JWT;


class Images extends Base
{   
    public function getPrice($action = 'undefined'){
        $price = 0;

        $method = request()->method();
        if ( strtolower($method) == 'post' ) {
            $data_string = file_get_contents('php://input');
            $params = json_decode($data_string,true);

            $price_data = [
                'controller'=>'images',
                'action'=>$action
            ];
            if ( !in_array($action,['expand']) ) {
                $price_data['model_name'] = empty($params['model_name']) ? "kling-v1" : strtolower($params['model_name']);
            }
            $price = Db::name('klingai_price')->where($price_data)->value('price');
            if (empty($price)) {
                exception('The product specification does not exist',5000);
            }

            $num = empty($params['n']) ? 1 : $params['n'];
            $price = round($price * $num , 3);
        }

        return $price;
    }

    // 图像生成
    public function generations(){
        $price = $this->getPrice();

        $result = $this->commonAction($price);
        echo $result;
    }

    // 多图参考生图
    public function multi_image2image(){
        $price = $this->getPrice('multi-image2image');

        $result = $this->commonAction($price);
        echo $result;
    }

    // 扩图
    public function editing_expand(){
        $price = $this->getPrice('expand');

        $result = $this->commonAction($price);
        echo $result;
    }

    // 虚拟试穿
    public function kolors_virtual_try_on(){
        $price = -1; // 人工核算

        $result = $this->commonAction($price);
        echo $result;
    }


}
